package com.example.controller;

import com.aliyun.core.http.HttpRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.*;
import com.example.service.AdminPlusService;
import com.example.service.AdminService;
import com.example.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * @author 刘文龙
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminPlusService adminPlusService;

    private static final Logger LOG = LoggerFactory.getLogger(AdminController.class);

    /**
     * 管理员角色登录接口
     * @param admin
     * @return ResponseEntity
     */
    @PostMapping
    @ResponseBody
    @RequestMapping("/admin-login")
    public ResponseEntity login(@RequestBody Admin admin){
        try {
            if (StringUtils.isEmpty(admin.getName())){
                LOG.error("用户名不能为空");
                return ResponseEntity.err("用户名不能为空");
            }
            if (StringUtils.isEmpty(admin.getPassword())){
                LOG.error("密码不能为空");
                return ResponseEntity.err("密码不能为空");
            }
            if (adminService.getAdminByPhone(admin.getName()) == null){
                LOG.error(admin.getName() + "无此用户");
                return ResponseEntity.err("无此用户");
            } else {
                // 登录校验业务
                Admin adm = adminService.getAdminByPAndP(admin.getName(), admin.getPassword());
                if (adm != null){
                    LOG.info("用户登录成功：" + admin.getName());
                    return  ResponseEntity.ok(adm);
                } else {
                    LOG.error(admin.getName() + "密码错误");
                    return ResponseEntity.err("密码错误");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            LOG.error("用户登录失败：" + admin.getName());
            return ResponseEntity.err(e.getMessage());
        }
    }

    /**
     *
     * 管理员角色查询信息接
     * @param info
     * @return PageEntity
     */

    @PostMapping
    @ResponseBody
    @RequestMapping(value = "/admin-getStudent")
    public PageEntity getAllStudent(@RequestBody PageInfo info){
        List<Student> data = adminService.getAllStudent(info);
        PageEntity p = new PageEntity();
        if(StringUtil.isNotEmpty(info.getSearch())) {
            return p.ok(data,info.getPage_size(),info.getPage_index(),adminService.getTotal(info.getSearch()));
        } else {
            return p.ok(data,info.getPage_size(),info.getPage_index(),adminService.getTotal());
        }
    }

    @DeleteMapping("/admin-deleteStudent/{id}")
    public ResponseEntity deleteStudent(@PathVariable String id){
        if(id == null){
            return ResponseEntity.err("id为空，删除失败！");
        }
        Boolean b =  adminService.deleteStudent(id);
        return ResponseEntity.ok(b);
    }

    /**
     * 分页
     * @param currentPage
     * @param pageSize
     * @param info
     * @return
     */
    @PostMapping
    @ResponseBody
    @RequestMapping(value = "/admin-getStudentInfo/{currentPage}/{pageSize}")
    public ResponseEntity getStudentInfo(@PathVariable Integer currentPage,@PathVariable Integer pageSize,
            @RequestBody StudentInformation info) {
        IPage<StudentInformation> page = adminPlusService.getPage(currentPage, pageSize, info);
        if (currentPage > page.getPages()) {
            page = adminPlusService.getPage((int) page.getPages(), pageSize, info);
        }
        return ResponseEntity.ok(page);
    }

    /**
     * 信息分页
     * @param currentPage
     * @param pageSize
     * @param info
     * @return
     */
    @PostMapping
    @ResponseBody
    @RequestMapping(value = "/admin-getStudentInfoAdmin/{currentPage}/{pageSize}")
    public ResponseEntity getStudentInfoAdmin(@PathVariable Integer currentPage,@PathVariable Integer pageSize,
                                         @RequestBody StudentInformation info) {
        IPage<StudentInformation> page = adminPlusService.getPage(currentPage, pageSize, info);
        if (currentPage > page.getPages()) {
            page = adminPlusService.getPage((int) page.getPages(), pageSize, info);
        }
        return ResponseEntity.ok(page);
    }

    @DeleteMapping("/admin-deleteStudentInfo/{id}")
    public ResponseEntity deleteStudentInfo(@PathVariable String id){
        if(id == null){
            return ResponseEntity.err("id为空，删除失败！");
        }
        Boolean b =  adminPlusService.removeById(id);
        return ResponseEntity.ok(b);
    }

//    /**
//     * 终审分页
//     * @param currentPage
//     * @param pageSize
//     * @param info
//     * @return
//     */
//    @PostMapping
//    @ResponseBody
//    @RequestMapping(value = "/admin-getStudentInfoAdmin/{currentPage}/{pageSize}")
//    public ResponseEntity getStudentFinalPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize,
//                                              @RequestBody StudentInformation info) {
//        IPage<StudentInformation> page = adminPlusService.getFinalPage(currentPage, pageSize, info);
//        if (currentPage > page.getPages()) {
//            page = adminPlusService.getFinalPage((int) page.getPages(), pageSize, info);
//        }
//        return ResponseEntity.ok(page);
//    }


 /**
     * 终审分页
     * @param pageSize
     * @return
     */
    @GetMapping
    @ResponseBody
    @RequestMapping(value = "/getStudentFinal/{currentPage}/{pageSize}")
    public PageEntity getStudentFinal(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        List<FinalPage> data = adminPlusService.getData((currentPage-1)*pageSize, pageSize);
        return PageEntity.ok(data,pageSize,currentPage,data.size());
    }

    @GetMapping
    @ResponseBody
    @RequestMapping("/setResult/{sid}/{s}")
    public ResponseEntity setResult(@PathVariable String sid, @PathVariable String s){
        return ResponseEntity.ok(adminPlusService.setResult(sid, s));
    }


}




